﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace WebC
{
    public partial class controls_pages : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            loadHtml();
        }

        void loadHtml()
        {
            string sHtml = "";
            try
            {
                if (!bAjax)
                {
                    int iPageCount = (int)((iRecordCount + iRecordNum - 1) / iRecordNum);
                    int iPageIndex = Convert.ToInt32(Request["page"]) <= 1 ? 1 : Convert.ToInt32(Request["page"]);

                    string sUrl = sPageUrl.IndexOf("?") < 0 ? sPageUrl + "?page=" : sPageUrl + "&page=";
                    string sBeforePage = iPageIndex == 1 ? "" : string.Format(@" <a href=""{0}{1}"">上一页</a>", sUrl, iPageIndex - 1);
                    string sNextPage = iPageIndex >= iPageCount ? "" : string.Format(@" <a href=""{0}{1}"">下一页</a>", sUrl, iPageIndex + 1);
                    sHtml = string.Format(@"<table width=""99%"" border=""0"" cellspacing=""1"" cellpadding=""1""  valign=""top"">
            <tr>
                <td align=""right"">总{4}页 {6}条 当前第{5}页 <a href=""{0}{1}"">首页</a>{2}{3} <a href=""{0}{4}"">尾页</a></td>
            </tr>
        </table>", sUrl, 1, sBeforePage, sNextPage, iPageCount, iPageIndex, iRecordCount);
                }
                else
                {
                    sHtml = @"<table width=""99%"" border=""0"" cellspacing=""1"" cellpadding=""1""  valign=""top"">
            <tr>
                <td align=""right"" id=""tdpageinfo"">页码信息载入中</td>
            </tr>
        </table>";
                }
                ltrPages.Text = sHtml;
            }
            catch
            {
                ltrPages.Text = "";
            }
        }

        bool UseAjax;
        int RecordNum;
        int RecordCount;
        string PageUrl;


        public bool bAjax
        {
            get
            {
                return UseAjax;
            }
            set
            {
                UseAjax = value;
            }
        }

        public int iRecordNum
        {
            get
            {
                return RecordNum;
            }
            set
            {
                RecordNum = value;
            }
        }

        public int iRecordCount
        {
            get
            {
                return RecordCount;
            }
            set
            {
                RecordCount = value;
            }
        }

        public string sPageUrl
        {
            get
            {
                return PageUrl;
            }
            set
            {
                PageUrl = value;
            }
        }
    }
}
